package com.dodayum.config;

import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.core.Map;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

import java.time.Duration;

/**
 * @Author BaiYu
 * </br>@Date 2025/1/3 09:25
 * @version 1.0
 * 备注：
 */
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("127.0.0.1:9200")
                .withConnectTimeout(Duration.ofSeconds(5))
                .withSocketTimeout(Duration.ofMinutes(1)).withClientConfigurer(httpClientBuilder -> customizeHttpClient((HttpAsyncClientBuilder) httpClientBuilder)
                )
                .build();
        return RestClients.create(clientConfiguration).rest();
    }


    private HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
        return httpClientBuilder
                .setMaxConnTotal(1000) // 设置最大连接数
                .setMaxConnPerRoute(200) // 设置每个路由的最大连接数
                ;
    }
}
